package com.cqut.forum.service.imp;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.cqut.forum.dao.PictureDao;
import com.cqut.forum.dao.PictureReplyDao;
import com.cqut.forum.entity.Picture;
import com.cqut.forum.entity.User;
import com.cqut.forum.service.PictureService;
import com.cqut.forum.util.MapKey;

/**
 * 图片service实现类
 * @author yangxun
 */
@Controller
public class PictureServiceImp implements PictureService {

	@Resource
	private PictureDao pictureDao;
	@Resource
	private PictureReplyDao pictureReplyDao;
	
	public List<Map<String, Object>> getByGroupId(int groupId, int index,
			int size,String orderCase,boolean asc) { 
		List<Map<String, Object>> pictures = pictureDao.getByGroupId(groupId, index, size,orderCase,asc);
		
		return pictures;
	}

	@Override
	public List<Map<String, Object>> getFriendPicture(int userId, int index,
			int size) {
		List<Map<String, Object>> pictures = pictureDao.getFriendsPicture(userId, index, size);
		return pictures;
	}

	@Override
	public List<Map<String, Object>> getLastPicture(int index,int size) {		
		List<Map<String, Object>> pictures = pictureDao.getLastPicture(index, size);
		return pictures;
	}

	@Override
	public boolean insert(List<Picture> picture) {
		boolean result = pictureDao.saveList(picture);
		return result;
	}

	@Override
	public Picture getById(int pictureId) {
		Picture picture = pictureDao.get(pictureId, Picture.class);
		return picture;
	}

	@Override
	public Picture update(Picture picture) {
		picture = pictureDao.update(picture);
		return picture;
	}

	@Override
	public List<Map<String, Object>> getMyAllPicture(int userId, int index,
			int size,String orderCase,boolean asc) {
		List<Map<String, Object>> pictures = pictureDao.getMyAllPicture(userId, index, size,orderCase,asc);
		
		return pictures;
	}

	@SuppressWarnings("deprecation")
	@Override
	public List<Map<String, Object>> getMyUnread(User user, int index, int size) {
		List<Picture> pictures = pictureDao.getMyUnread(user.getUserId(), index, size);
		//定义图片评论容器
		List<Map<String, Object>> pictureReplys = new ArrayList<Map<String,Object>>();
		//获取图片评论内容
		if(pictures.size()>0){
			List<Integer> pictureIds = new ArrayList<Integer>();
			for(int i=0;i<pictures.size();i++){
				pictureIds.add(pictures.get(i).getPictureId());
			}
			pictureReplys = pictureReplyDao.getPictureReplys(pictureIds);
		}		
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		if(pictures.size()==0){
			return result;
		}
		
		for(Picture p:pictures){
			Map<String, Object> m = new HashMap<String, Object>();
			
			m.put("image", user.getUserPhoto());
			m.put("name", user.getUserName());
			m.put("pictureId", p.getPictureId());
			m.put("content", p.getPictureAddress());
			m.put("time", p.getUploadTime().toLocaleString());
			List<Map<String, Object>> reply = new ArrayList<Map<String,Object>>();
			for(Map<String, Object> r:pictureReplys){
				if(p.getPictureId().equals(r.get("picture_id"))){
					Map<String, Object> rep = new HashMap<String, Object>();
					rep.put("image", r.get("user_photo"));
					rep.put("name", r.get("user_name"));
					rep.put("content", r.get("review_contents"));
					rep.put("time", ((Date)r.get("review_time")).toLocaleString());
					reply.add(rep);
				}
			}
			m.put("reply", reply);
			result.add(m);
		}
		return result;
	}

	@SuppressWarnings("deprecation")
	@Override
	public List<Map<String, Object>> getHotPicture(int index, int size) {
		List<Map<String, Object>> pictures = pictureDao.getHotPicture(index,size);
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		
		for(Map<String, Object> map:pictures){
			Map<String, Object> m = new HashMap<String, Object>();
			m.put("userName", map.get("user_name"));
			m.put("userPhoto", map.get("user_photo"));
			m.put("pictureSrc", map.get("picture_address"));
			m.put("reviewTimes", map.get("review_times"));
			m.put("pictureId", map.get("picture_id"));
			m.put("uploadTime", ((Date)map.get("upload_time")).toLocaleString());
			
			result.add(m);
		}
		return result;
	}

	@Override
	public int getPictureIndex(int groupId,int pictureId) {
		int index = pictureDao.getPictureIndex(groupId,pictureId);
		return index;
	}

	@Override
	public Picture deleteById(int pictureId) {
		Picture picutre = pictureDao.delete(pictureId, Picture.class);
		return picutre;
	}

	@Override
	public List<Map<String, Object>> getByLikeName(String groupName, int index,
			int size) {
		List<Map<String, Object>> pictures = pictureDao.getByLikeName(groupName,index,size);
		return pictures;
	}

	@Override
	public List<Map<String, Object>> loadPicture(String pictureOwner,
			String pictureType, String uploadTime, int index, int size) {
		List<Map<String, Object>> pictures = pictureDao.loadPicture(pictureOwner,pictureType,uploadTime,index,size);
		List<Map<String, Object>> result = MapKey.changeMapKey(pictures);
		
		return result;
	}

	@Override
	public boolean updatePictureState(int pictureId, boolean state) {
		pictureDao.updatePictureState(pictureId,state);
		return true;
	}

	@Override
	public User getPictureOwner(int pictureId) {
		User user = pictureDao.getPictureOwner(pictureId);
		return user;
	}

}
